import json

from models.base import Cards


class CardPool:
    """
    卡池的实体模型
    """

    def __init__(self, pool_name):

        self.name = pool_name
        self.role_rarity_6 = []
        self.num_all = 0
        # 当前的抽数，出6星后归零
        self.num_current = 0

        self.num_rarity_6 = 0
        self.num_rarity_5 = 0
        self.num_rarity_4 = 0
        self.num_rarity_3 = 0

    def __str__(self):
        return f"卡池：{self.name}  共：{self.num_all}抽 \n" \
               f"卡池：{self.name}  六星角色：{self.role_rarity_6} \n" \
               f"卡池：{self.name}  6星-{self.num_rarity_6}个，平均{self.num_all / self.num_rarity_6 if self.num_rarity_6 != 0 else 0}抽一个6星，已经{self.num_current}抽没出6星了\n" \
               f""

    def data_process_obj(self, org_data: Cards):
        data = json.loads(org_data.data)
        for role in data:
            self.num_all += 1
            self.num_current += 1
            # 官网的接口定义稀有度都小一位
            if role["rarity"] == 5:
                self.num_rarity_6 += 1
                self.role_rarity_6.append({
                    "name": role["name"],
                    "num_current": self.num_current,
                    "is_new": role["isNew"],
                    "create_time": org_data.create_time

                })
                self.num_current = 0

            elif role["rarity"] == 4:
                self.num_rarity_5 += 1
            elif role["rarity"] == 3:
                self.num_rarity_4 += 1
            elif role["rarity"] == 2:
                self.num_rarity_3 += 1

    def data_process(self, data):

        for role in data["chars"]:
            self.num_all += 1
            self.num_current += 1
            # 官网的接口定义稀有度都小一位
            if role["rarity"] == 5:
                self.num_rarity_6 += 1
                self.role_rarity_6.append({role["name"]: self.num_current})
                self.num_current = 0

            elif role["rarity"] == 4:
                self.num_rarity_5 += 1
            elif role["rarity"] == 3:
                self.num_rarity_4 += 1
            elif role["rarity"] == 2:
                self.num_rarity_3 += 1
